package cn.test.future.cf;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Supplier;

/**
 * 结果组合测试
 */
@Slf4j
public class CompletableFutureTest5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFutureTest5 test5 = new CompletableFutureTest5();
        test5.thenCombine();
    }

    public void thenCombine() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("第一个任务：" + num);
                return num;
            }
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("第二个任务：" + num);
                return num;
            }
        });

        CompletableFuture<Integer> result = future.thenCombine(future2, new BiFunction<Integer, Integer, Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        });

        System.out.println("任务合并最终结果：" + result.join());
    }

}
